package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.Coaches;
import com.ruoyi.system.mapper.CoursesMapper;
import com.ruoyi.system.domain.Courses;
import com.ruoyi.system.service.ICoursesService;

/**
 * 健身课程信息Service业务层处理
 * 
 * @author 张英旺
 * @date 2024-02-27
 */
@Service
public class CoursesServiceImpl implements ICoursesService 
{
    @Autowired
    private CoursesMapper coursesMapper;

    /**
     * 查询健身课程信息
     * 
     * @param courseId 健身课程信息主键
     * @return 健身课程信息
     */
    @Override
    public Courses selectCoursesByCourseId(Long courseId)
    {
        return coursesMapper.selectCoursesByCourseId(courseId);
    }

    /**
     * 查询健身课程信息列表
     * 
     * @param courses 健身课程信息
     * @return 健身课程信息
     */
    @Override
    public List<Courses> selectCoursesList(Courses courses)
    {
        return coursesMapper.selectCoursesList(courses);
    }

    /**
     * 新增健身课程信息
     * 
     * @param courses 健身课程信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCourses(Courses courses)
    {
        courses.setCreateTime(DateUtils.getNowDate());
        int rows = coursesMapper.insertCourses(courses);
        insertCoaches(courses);
        return rows;
    }

    /**
     * 修改健身课程信息
     * 
     * @param courses 健身课程信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCourses(Courses courses)
    {
        courses.setUpdateTime(DateUtils.getNowDate());
        coursesMapper.deleteCoachesByCoachId(courses.getCourseId());
        insertCoaches(courses);
        return coursesMapper.updateCourses(courses);
    }

    /**
     * 批量删除健身课程信息
     * 
     * @param courseIds 需要删除的健身课程信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCoursesByCourseIds(Long[] courseIds)
    {
        coursesMapper.deleteCoachesByCoachIds(courseIds);
        return coursesMapper.deleteCoursesByCourseIds(courseIds);
    }

    /**
     * 删除健身课程信息信息
     * 
     * @param courseId 健身课程信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCoursesByCourseId(Long courseId)
    {
        coursesMapper.deleteCoachesByCoachId(courseId);
        return coursesMapper.deleteCoursesByCourseId(courseId);
    }

    /**
     * 新增教练信息信息
     * 
     * @param courses 健身课程信息对象
     */
    public void insertCoaches(Courses courses)
    {
        List<Coaches> coachesList = courses.getCoachesList();
        Long courseId = courses.getCourseId();
        if (StringUtils.isNotNull(coachesList))
        {
            List<Coaches> list = new ArrayList<Coaches>();
            for (Coaches coaches : coachesList)
            {
                coaches.setCoachId(courseId);
                list.add(coaches);
            }
            if (list.size() > 0)
            {
                coursesMapper.batchCoaches(list);
            }
        }
    }
}
